package service

import (
	"RocFamilySystem/internal/repository"
	"RocFamilySystem/models"
	"RocFamilySystem/tools/excel"
	"fmt"
	"github.com/xuri/excelize/v2"
	"log"
	"mime/multipart"
)

type WorkTravelService struct {
	WorkTravelRepository *repository.WorkTravelRepository
}

// 初始化
func init() {
	registerContainer["WorkTravelService"] = &WorkTravelService{}
}

// SetRepository 设置持久层
func (service *WorkTravelService) SetRepository(workTravelRepository *repository.WorkTravelRepository) {
	service.WorkTravelRepository = workTravelRepository
}

// AddWorkTravel 添加工作出差
func (service *WorkTravelService) AddWorkTravel(workTravel *models.WorkTravel, loginUserId int) {

	workTravel.CreateBy = &loginUserId
	service.WorkTravelRepository.InsertWorkTravel(workTravel)

}

// RemoveWorkTravel 删除工作出差
func (service *WorkTravelService) RemoveWorkTravel(workTravelId int, loginUserId int) {
	service.WorkTravelRepository.DeleteWorkTravel(workTravelId, loginUserId)
}

// ModifyWorkTravel 修改工作出差
func (service *WorkTravelService) ModifyWorkTravel(workTravel *models.WorkTravel, loginUserId int) {
	service.WorkTravelRepository.UpdateWorkTravel(workTravel, loginUserId)
}

// QueryWorkTravel 查询工作出差
func (service *WorkTravelService) QueryWorkTravel(workTravel *models.WorkTravel, pageNum, pageSize int, loginUserId int) PageInfo {
	tables, total := service.WorkTravelRepository.SelectWorkTravel(workTravel, pageNum, pageSize, loginUserId)
	return toPageInfo(pageNum, pageSize, total, tables)
}

// QueryWorkTravelById 通过ID查询工作出差
func (service *WorkTravelService) QueryWorkTravelById(id int, loginUserId int) models.WorkTravel {
	return service.WorkTravelRepository.SelectWorkTravelById(id, loginUserId)
}

// RemoveWorkTravelBatch 批量删除工作出差
func (service *WorkTravelService) RemoveWorkTravelBatch(ids []string, loginUserId int) {
	service.WorkTravelRepository.DeleteWorkTravelBatch(ids, loginUserId)
}

// 工作簿名称
var workTravelSheetName = "工作出差"

// 属性位置信息以及顺序，导出的excel设置
var workTravelPositions = []excel.ValuePosition{

	{fmt.Sprintf("%c", 'A'+0), "WorkAddrr", "出差地点"},
	{fmt.Sprintf("%c", 'A'+1), "Reason", "出差事由"},
	{fmt.Sprintf("%c", 'A'+2), "StartEndTime", "起止时间"},
	{fmt.Sprintf("%c", 'A'+3), "CreateTime", "创建时间"},
}

// WorkTravelTemplateDownload 工作出差模板下载
func (service *WorkTravelService) WorkTravelTemplateDownload() *excelize.File {
	file := excelize.NewFile()
	excel.GeneratorHeader(file, workTravelSheetName, workTravelPositions)
	return file
}

// AddWorkTravelBatch 批量添加工作出差
func (service *WorkTravelService) AddWorkTravelBatch(uploadFile *multipart.FileHeader) {
	file, _ := uploadFile.Open()
	defer file.Close()
	excelFile, err := excelize.OpenReader(file)
	if err != nil {
		log.Panicln("模板文件错误！", err.Error())
	}
	var list []models.WorkTravel
	excel.ReadExcel(excelFile, workTravelSheetName, workTravelPositions, 2, &list)
	service.WorkTravelRepository.InsertWorkTravelBatch(list)
}

// WorkTravelExport 导出工作出差
func (service *WorkTravelService) WorkTravelExport(ids []string) *excelize.File {
	list := service.WorkTravelRepository.SelectWorkTravelByIds(ids)
	file := excelize.NewFile()
	excel.GeneratorHeader(file, workTravelSheetName, workTravelPositions)
	excel.WriterExcel(file, workTravelSheetName, workTravelPositions, 2, &list)
	return file
}
